<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>预约管理<small>检查项管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>预约管理</el-breadcrumb-item>
            <el-breadcrumb-item>检查项管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="项目编码/项目名称" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>


                <el-table :data="dataList" border style="width: 100%">

                    <el-table-column fixed prop="code" label="编码" width="200"></el-table-column>

                    <el-table-column prop="name" label="姓名" width="200"></el-table-column>

                    <el-table-column prop="sex" label="性别" width="200"></el-table-column>

                    <el-table-column fixed="right" label="操作" width="200" style="text-align: center">
                        <template slot-scope="scope">

                            <el-button type="primary" @click="handleUpdate(scope.row)" size="medium">编辑</el-button>
                            <el-button type="danger" size="medium" @click="handleDelete(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <!--表格分页显示-->
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
        </div>
    </div>

    <!--新增弹窗区域-->
    <div>
        <el-dialog title="新增检查项" :visible.sync="dialogFormVisible">
            <el-form :rules="rules" :model="formData" ref="addForm">
                <el-form-item label="编码" prop="code">
                    <el-input v-model="formData.code"></el-input>
                </el-form-item>
                <el-form-item label="名称" prop="name">
                    <el-input v-model="formData.name"></el-input>
                </el-form-item>

                <el-form-item label="性别">
                    <el-select v-model="formData.sex" placeholder="请选择">
                        <el-option value="1" label="男"></el-option>
                        <el-option value="0" label="女"></el-option>
                    </el-select>
                </el-form-item>

            </el-form>

            <el-button type="primary" @click="handleAdd">提交</el-button>
        </el-dialog>
    </div>

    <!--编辑弹窗区域-->
    <div>
        <el-dialog title="编辑检查项" :visible.sync="dialogFormVisible4Edit">
            <el-form :rules="rules" :model="formData">
                <el-form-item label="编码" prop="code">
                    <el-input v-model="formData.code" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="名称" prop="name">
                    <el-input v-model="formData.name"></el-input>
                </el-form-item>

                <el-form-item label="性别">
                    <el-select v-model="formData.sex" placeholder="请选择">
                        <el-option value="1" label="男"></el-option>
                        <el-option value="0" label="女"></el-option>
                    </el-select>
                </el-form-item>

            </el-form>

            <el-button type="primary" @click="handleEdit">提交</el-button>
        </el-dialog>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            rules: {//校验规则
                code: [{required: true, message: '项目编码为必填项', trigger: 'blur'}],
                name: [
                    {required: true, message: '项目名称为必填项', trigger: 'blur'},
                    /* {
                         validator:(rule,value,callback)=>{
                             //value:待校验的内容，callback:校验框架的回调
                             if (value == '爸爸') {
                                 callback();  //校验成功就不填
                             }else {
                                 callback("叫爸爸"); //检验失败，弹出提示信息
                             }

                         }
                     }*/
                ]
            }
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
        },
        methods: {
            //编辑
            handleEdit() {
                axios.post("/checkitem/edit.do", this.formData).then((res) => {
                    this.dialogFormVisible4Edit = false;

                    this.$message({
                        message: res.data.message,
                        type: res.data.flag ? 'success' : 'error'  //	success/warning/info/error
                    });

                    this.findPage();
                }).catch((error) => {
                    console.log(error);
                })
            },
            //添加
            handleAdd() {
                this.$refs.addForm.validate((flag) => {
                    if (flag) {
                        //校验成功
                        //获取表单数据，然后使用axios提交数据
                        // console.log(this.formData);

                        axios.post("/checkitem/add.do", this.formData).then((res) => {
                            if (res.data.flag) {
                                this.dialogFormVisible = false;
                                //TODO 刷新页面
                            }
                            this.$message({
                                message: res.data.message,
                                type: res.data.flag ? 'success' : 'error'  //	success/warning/info/error
                            });
                        }).catch((error) => {
                            console.log(error);
                        });
                    } else {
                        //校验失败,提示用户
                        this.$message({
                            message: '表单校验失败',
                            type: 'error'  //	success/warning/info/error
                        });
                    }

                });

                //执行表单校验，


            },
            //分页查询
            findPage() {
                // console.log(this.pagination);
                axios.post("/checkitem/findPage.do", this.pagination).then((res) => {
                    // console.log(res);
                    this.pagination.total = res.data.total;
                    this.dataList = res.data.rows;
                }).catch((error) => {
                    console.log(error);
                })
            },
            // 重置表单
            resetForm() {
            },
            // 弹出添加窗口
            handleCreate() {
                //弹出新增的弹窗
                this.dialogFormVisible = true;
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                axios.get("/checkitem/findById.do", {
                    params: {
                        id: row.id
                    }
                }).then((res) => {
                    //console.log(res);
                    this.formData = res.data.data;
                    this.dialogFormVisible4Edit = true;
                }).catch((error) => {
                    console.log(error);
                });
                //console.log(row);
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage()
            },
            // 删除
            handleDelete(row) {
                this.$confirm('确定删除吗？删除不可恢复哦', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }
                ).then(() => {
                    axios.get("/checkitem/delete.do", {
                        params: {
                            id: row.id
                        }
                    }).then((res) => {

                        this.$message({
                            message: res.data.message,
                            type: res.data.flag ? 'success' : 'error'  //	success/warning/info/error
                        });

                            this.findPage();

                    })

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }
        }
    })
</script>
</html>